package vasys.mehaniksspacemars;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;

import java.util.Random;

public class MarsListener implements Listener {
    @EventHandler
    public void onChunkLoad(ChunkLoadEvent event) {
        if(event.getChunk().load() && event.getWorld().getName().equals("mehaniks_mars") && (!event.getWorld().getBiome((event.getChunk().getX() * 16), 64, (event.getChunk().getZ() * 16)).equals(Biome.CUSTOM) || !event.getWorld().getBiome((event.getChunk().getX() * 16), -64, (event.getChunk().getZ() * 16)).equals(Biome.CUSTOM))) {
            String fillV = "execute in mehaniks_mars run fillbiome " + (event.getChunk().getX() * 16) + " 64 " + (event.getChunk().getZ() * 16) + " " + (event.getChunk().getX() * 16 + 15) + " 319 " + (event.getChunk().getZ() * 16 + 15) + " mehaniks:mars_vales";
            String fillC = "execute in mehaniks_mars run fillbiome " + (event.getChunk().getX() * 16) + " -64 " + (event.getChunk().getZ() * 16) + " " + (event.getChunk().getX() * 16 + 15) + " 63 " + (event.getChunk().getZ() * 16 + 15) + " mehaniks:mars_caves";
            Bukkit.getScheduler().scheduleSyncDelayedTask(MehaniksSpaceMars.getPlugin(MehaniksSpaceMars.class), ()-> {
                Entity entity = event.getWorld().spawnEntity(new Location(event.getWorld(), (event.getChunk().getX() * 16), 320, (event.getChunk().getZ() * 16)), EntityType.MINECART_COMMAND);
                Bukkit.dispatchCommand(entity, fillV);
                Bukkit.dispatchCommand(entity, fillC);
                entity.remove();
            }, 0);
        }

        Random random = new Random();
        if(event.getChunk().load() && event.getWorld().getName().equals("mehaniks_mars") && event.isNewChunk() && random.nextInt(10001) >= 9982) {
            float noise2 = (CustomChunkGenerator.terrainNoise.GetNoise((event.getChunk().getX() * 16), (event.getChunk().getZ() * 16)) * 2) + (CustomChunkGenerator.detailTerrainNoise.GetNoise((event.getChunk().getX() * 16), (event.getChunk().getZ() * 16)) / 10);
            float noise4 = (CustomChunkGenerator.addedTerrainNoise.GetNoise((event.getChunk().getX() * 16), (event.getChunk().getZ() * 16)) * 2) + (CustomChunkGenerator.detailTerrainNoise.GetNoise((event.getChunk().getX() * 16), (event.getChunk().getZ() * 16)) / 10);
            int currentY = Math.round((224 + ((noise2 * 32) + (noise4 * 2))));
            int rRot = random.nextInt(4);
            String rotation = "rotation:\"NONE\"";
            if (rRot == 0) rotation = "rotation:\"CLOCKWISE_90\",";
            if (rRot == 1) rotation = "rotation:\"CLOCKWISE_180\",";
            if (rRot == 2) rotation = "rotation:\"COUNTERCLOCKWISE_90\",";
            String setSB;
            String setRB;
            String structure;

            int ramdomer = random.nextInt(11);
            if (ramdomer < 1) {
                structure = "mehaniks:mars_bunker";
                setSB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-16) + " " + (event.getChunk().getZ() * 16) + " structure_block{mode:\"LOAD\"," + rotation + "name:\"" + structure + "\"}";
                setRB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-15) + " " + (event.getChunk().getZ() * 16) + " redstone_block";
            } else if (ramdomer < 4) {
                structure = "mehaniks:mars_food_station";
                setSB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-2) + " " + (event.getChunk().getZ() * 16) + " structure_block{mode:\"LOAD\"," + rotation + "name:\"" + structure + "\"}";
                setRB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-1) + " " + (event.getChunk().getZ() * 16) + " redstone_block";
            } else if (ramdomer < 7) {
                structure = "mehaniks:mars_oil_station";
                setSB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-2) + " " + (event.getChunk().getZ() * 16) + " structure_block{mode:\"LOAD\"," + rotation + "name:\"" + structure + "\"}";
                setRB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-1) + " " + (event.getChunk().getZ() * 16) + " redstone_block";
            } else {
                structure = "mehaniks:mars_oxygen_station";
                setSB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-2) + " " + (event.getChunk().getZ() * 16) + " structure_block{mode:\"LOAD\"," + rotation + "name:\"" + structure + "\"}";
                setRB = "setblock " + (event.getChunk().getX() * 16) + " " + (currentY-1) + " " + (event.getChunk().getZ() * 16) + " redstone_block";
            }


            Bukkit.getLogger().info("Load " + structure + " rotation: " + rotation + " at " + (event.getChunk().getX() * 16) + " " + Math.round(currentY) + " " + (event.getChunk().getZ() * 16));

            Bukkit.getScheduler().scheduleSyncDelayedTask(MehaniksSpaceMars.getPlugin(MehaniksSpaceMars.class), ()-> {
                Entity entity = event.getWorld().spawnEntity(new Location(event.getWorld(), (event.getChunk().getX() * 16), 320, (event.getChunk().getZ() * 16)), EntityType.MINECART_COMMAND);
                Bukkit.dispatchCommand(entity, setSB);
                Bukkit.dispatchCommand(entity, setRB);
                entity.remove();
            }, 0);
        }
    }
}